import com.alibaba.fastjson.JSON;

import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
import org.springframework.security.crypto.bcrypt.BCrypt;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.jwt.Jwt;
import org.springframework.security.jwt.JwtHelper;
import org.springframework.security.jwt.crypto.sign.RsaSigner;
import org.springframework.security.jwt.crypto.sign.RsaVerifier;
import org.springframework.security.rsa.crypto.KeyStoreKeyFactory;
import org.springframework.util.Base64Utils;

import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.security.KeyPair;
import java.security.interfaces.RSAPrivateCrtKey;
import java.security.interfaces.RSAPrivateKey;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * 令牌的加密和解析
 */
public class JWTTestDemo {

    @Test
    public void createJwtToken() {
        // 加载证书 读取证书数据 获取私钥
        ClassPathResource jksFile = new ClassPathResource("dunjiaxing.jks");
        //读取证书 parm1 证书文件 parm2 密码 char类型
        KeyStoreKeyFactory keyStoreKeyFactory = new KeyStoreKeyFactory(jksFile, "dunjiaxing".toCharArray());
        // 获取证书中的一对秘钥  parm1  证书别名   param 2 证书密码
        KeyPair keyPair = keyStoreKeyFactory.getKeyPair("dunjiaxing", "dunjiaxing".toCharArray());
        // 私钥
        RSAPrivateKey aPrivate = (RSAPrivateKey) keyPair.getPrivate();


        // 设置载荷
        Map<String, Object> payload = new HashMap<>();
        payload.put("nickName", "dunjiaxing");
        payload.put("sex", "男");
        payload.put("address", "石家庄");
        System.out.println();


        Jwt jwt = JwtHelper.encode(JSON.toJSONString(payload), new RsaSigner(aPrivate));
        String token = jwt.getEncoded();
        System.out.println(token);


    }


    @Test
    public void parseJwtToken() {

//        String publicKey = "-----BEGIN PUBLIC KEY-----MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAmp50Phh7GRPkt8LhxuDKGCtqWwNIDIf80YO1l0rgopykl06QHmvTynUfcXWV11gSk4QR/B8tXBE7Zz+h8GPNy3z8AbNcnuEgmVQTi2l1N08so4Mw7a03YuhpH7S53d7AObH77xT692vQEWlR3G5zcsii1M95qoRLHDNpgBJcjYdK6JPUIz2Owpc44G8W6PFaUpJz/Ky4R19BwXMxjaGe3VZhVF0M/Nf05ncvJTyvA6LVg2pv4apxvUgA6lTDLeisXqRjHcS79LV7vBrBnPjCQ/cdD4Wqrr4yA3ZlfXjlDscnUE0/oWIFpk0XC+tVCvWq4AJP68DW6rTR75spSsc4WQIDAQAB-----END PUBLIC KEY-----";
//        String token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJhZGRyZXNzIjoi55-z5a625bqEIiwibmlja05hbWUiOiJkdW5qaWF4aW5nIiwic2V4Ijoi55S3In0.FpiQN1P_HKNk5USrTLtzLqLLE9FwnRIi0uREYsj5BrMch5EICUEBiVbvRjkdosNBmC22LI_8wMtp2FudvOXR8FhQNX7FM650xNqR5nql0-UO6rbql5UjPc6xQwK1EuDYy-VRiin21KfiScEG4xVfyIiJkj20ChwNRXthpD1Cqaa8uGBxuG-A1rpj4NOHBkojoCSFj8wNGbAIOBppkEFYjMVgKm5z5pjU7kToprVLhHvzizLHPviY8cnKekOUkhmN6Z2xJNWSVAKTZnV5jprroUmQsTAuopW11TBUiS-PPDr7-WdwCbRNsMk69V1INriGd4h84a2kzFDcBd-htvYF0g";
//        Jwt jwt = JwtHelper.decodeAndVerify(token,
//                new RsaVerifier(publicKey));
//        String parse = jwt.getClaims();
//        System.out.println("解密为:" + parse);


        String dunjiaxing = new BCryptPasswordEncoder().encode("123456");
        System.out.println("---"+dunjiaxing);

        boolean checkpw = BCrypt.checkpw("$2a$10$uEnDJp9UROVqvRGyE3fxqudpQ3QQ59cgii3a/lYf3lrUuiGlQ3cZG".toString(), "$2a$10$cBt/jbSUzbkZtuaeFigeLu8dtpF7qDy3U8LfSas0JVkhbj9mYSjY.");
        System.out.println(checkpw);
    }

    @Test
    public  void parseStr() throws UnsupportedEncodingException {
        String c = "ZHVuamlheGluZzpkdW5qaWF4aW5n";

        byte[] decode = Base64Utils.decode(c.getBytes(StandardCharsets.UTF_8));
        System.out.println(new String(decode,"UTF-8"));
    }

}
